define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    // 动态修改selectpage的data-source
    function onchange_jump_type(jump_type)
    {
        let jump_url,jump_id,jump_id_text_obj;
        jump_url = $(".jump_url");
        jump_id = $(".jump_id");
        jump_id_text_obj = $("#c-jump_id_text").data("selectPageObject");
        switch (jump_type){
            case "none":
                jump_url.addClass("hide");
                jump_id.addClass("hide");
                break;
            case "case":
                jump_url.addClass("hide");
                jump_id.removeClass("hide");
                break;
            case "web":
                jump_url.removeClass("hide");
                jump_id.addClass("hide");
                break;
        }
        if(jump_id_text_obj){
            $("#c-jump_id").selectPageClear();
            jump_id_text_obj.option.pageSize = 5;
            switch (jump_type){
                case "case":
                    jump_id_text_obj.option.data = "project/project/index";
                    jump_id_text_obj.option.showField = "title";
                    jump_id_text_obj.option.searchField = ["title"];
                    // jump_id_text_obj.option.orderBy[0] = ["title","ASC"];
                    break;
            }
        }
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ads/ads/index' + location.search,
                    add_url: 'ads/ads/add',
                    edit_url: 'ads/ads/edit',
                    del_url: 'ads/ads/del',
                    multi_url: 'ads/ads/multi',
                    import_url: 'ads/ads/import',
                    table: 'ads',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'position_id', title: __('Position_id'), visible:false, searchList: $.getJSON("ads/position/getSearchList")},
                        {field: 'position.name', title: __('Position.name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'image_wap', title: __('Image_wap'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'jump_type', title: __('Jump_type'), searchList: {"none":__('Jump_type none'),"case":__('Jump_type case'),"web":__('Jump_type web')}, formatter: Table.api.formatter.normal},
                        // {field: 'jump_url', title: __('Jump_url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        // {field: 'project.title', title: __('Project.title'), operate: 'LIKE'},
                        // {field: 'jump_id', title: __('Jump_id')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-jump_type").change(function (){
                let jump_type = $(this).val();
                onchange_jump_type(jump_type);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            onchange_jump_type($("#c-jump_type").val());
            $("#c-jump_type").change(function (){
                let jump_type = $(this).val();
                onchange_jump_type(jump_type);
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
